#include "StdAfx.h"
#include "stdlib.h"
#include "TransMan.h"

#include "TG_NetSDK.h"
#include "TG_NET_TGC_DEF.h"

TransMan::TransMan(void)
{
	m_hLogin = NULL;
	memset(m_szCServerIP,0,sizeof(m_szCServerIP));
	m_nCSPort = 0;

	m_nActiveTime = 5000;

	m_hSvrNet   = NULL;

	m_nCmdPort  = 12000;
	m_nDataPort = 12001;

	
	m_nUseCount = 0;
	for(int i=0;i<TG_TS_MAXCAMERA;i++)
	{
		m_arCamera[i] = NULL;
	}

	m_hAddRsInfoEvent = NULL;


	m_hSvrTrans = NULL;
}

TransMan::~TransMan(void)
{
	TransStop();

	TransStop_new();
}

VOID TransMan::InitConfig()
{
	char chFileName[MAX_PATH] = {0};
	sprintf(chFileName,".\\ts_cfg.ini");
	char szip[MAX_PATH] ={0};

	m_nCSEnable = ::GetPrivateProfileInt(_T("CS_SVR"),_T("cs_enable"),0, chFileName);
	GetPrivateProfileString(_T("CS_SVR"),_T("cs_ip"),_T("192.168.1.117"),m_szCServerIP,TG_IPADDR_MAX,chFileName);
	m_nCSPort = ::GetPrivateProfileInt(_T("CS_SVR"),_T("cmdport"),12001, chFileName);
	m_nActiveTime = ::GetPrivateProfileInt(_T("CS_SVR"),_T("activetimems"),5000, chFileName);

	m_nTransServeID  = ::GetPrivateProfileInt(_T("TS_SVR"),_T("ts_id"),100, chFileName);
	m_nCmdPort       = ::GetPrivateProfileInt(_T("TS_SVR"),_T("cmdport"),12000, chFileName);
	m_nDataPort      = ::GetPrivateProfileInt(_T("TS_SVR"),_T("dataport"),12001, chFileName);
}


void TransMan::TransStop()
{
	if( NULL != m_hLogin )
	{
		TG_Net_Logout( m_hLogin );
		m_hLogin = NULL;
	}

	if( NULL != m_hSvrNet )
	{
		TG_SvrNet_Stop( m_hSvrNet );

		TG_SvrNet_Cleanup( m_hSvrNet );
		m_hSvrNet = NULL;
	}
	
	if( NULL != m_hAddRsInfoEvent )
	{
		SetEvent( m_hAddRsInfoEvent );
	}
	TGThread::TGThdUnInit();

	MySafeCloseHandle( m_hAddRsInfoEvent );
}


int TransMan::TransStart()
{
	int nRet = TG_ERR_FAILURE;

	do 
	{
		if( NULL == m_hSvrNet )
		{
			TG_SvrNet_Init( m_hSvrNet );
		}
		if( NULL == m_hSvrNet )
		{
			break;
		}

		TG_SvrNet_RegRealStreamCallBack( m_hSvrNet,SvrNetRealStreamCB,this);
		TG_SvrNet_SetConnectTime(m_hSvrNet,5000,20);
		TG_SvrNet_SetListen(m_hSvrNet,NULL, m_nCmdPort, m_nDataPort);

		m_hAddRsInfoEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
		if( NULL == m_hAddRsInfoEvent )
		{
			nRet = TG_ERR_FAILURE;
			break;
		}
		ResetEvent(m_hAddRsInfoEvent);


		nRet = TG_SvrNet_Start( m_hSvrNet );
		if( TG_ERR_SUCCESS != nRet )
		{
			break;
		}


		if( 1 == m_nCSEnable )
		{
			TG_LOGIN_INFO xLoinInfo;
			sprintf(xLoinInfo.xConnectInfo.szServerIP,m_szCServerIP);
			xLoinInfo.xConnectInfo.nServerPort = m_nCSPort;

			nRet = TG_Net_Login( &xLoinInfo, m_hLogin );
			if( TG_ERR_SUCCESS != nRet )
			{
				break;
			}

			UpdateServerLive();
		}


		if( !TGThread::TGThdInit() )
		{
			nRet = TG_ERR_FAILURE;
			break;
		}

		return TG_ERR_SUCCESS;

	} while (FALSE);

	return nRet;
}


TGERR_CODE TransMan::SvrNetRealStreamCB(TG_SVR_RS_INFO *pRSInfo,VOID *pContext)
{
	( ( TransMan*)pContext )->SvrNetRealStream( pRSInfo );

	return TG_ERR_SUCCESS;
}

TGERR_CODE TransMan::SvrNetRealStream(TG_SVR_RS_INFO *pRSInfo)
{
	TG_AUTO_LOCK xLock(&m_xRSSect);

	m_arRSInfo.push_back( *pRSInfo );
	SetEvent( m_hAddRsInfoEvent );

	return TG_ERR_SUCCESS;
}

BOOL TransMan::GetRSInfo(TG_SVR_RS_INFO &xRSInfo)
{
	TG_AUTO_LOCK xLock(&m_xRSSect);

	UINT32 nCount = (UINT32)m_arRSInfo.size();
	if( nCount <= 0 )
	{
		return FALSE;
	}

	xRSInfo = m_arRSInfo[0];

	m_arRSInfo.erase( m_arRSInfo.begin() );

	return TRUE;
}

int TransMan::TGThdDispatch(HANDLE hExitEvent)
{
	HANDLE hEvent[2];
	hEvent[0] = hExitEvent;
	hEvent[1] = m_hAddRsInfoEvent;

	TG_SVR_RS_INFO xRSInfo;

	DWORD  dwWait = 0;
	while( TGThread::m_bThreadRun )
	{
        dwWait = WaitForMultipleObjects(2,hEvent,FALSE,INFINITE);
		if( (WAIT_OBJECT_0+1) != dwWait || !TGThread::m_bThreadRun )
		{
			break;
		}

        if( !GetRSInfo( xRSInfo ) )
		{
			ResetEvent( m_hAddRsInfoEvent );
			continue;
		}
		
		DoRealStream( &xRSInfo );
	}

	return 0;
}


inline SINT16 TransMan::GetCameraPos( TG_DEV_INFO *pDevInfo )
{
	SINT16 nCameraPos = -1;
	for(UINT16 i=0;i<m_nUseCount;i++)
	{
		if( NULL != m_arCamera[i] )
		{
			if( 0 == memcmp(pDevInfo,&(m_arCamera[i]->m_xDevInfo),sizeof(TG_DEV_INFO) ) )
			{
				nCameraPos = i;
				break;
			}
		}
	}
	return nCameraPos;
}

inline TransRealStream* TransMan::GetCameraPoint( TG_DEV_INFO *pDevInfo )
{
	TransRealStream *pCamera = NULL;
	for(UINT16 i=0;i<m_nUseCount;i++)
	{
		if( NULL != m_arCamera[i] )
		{
			if( 0 == memcmp(pDevInfo,&(m_arCamera[i]->m_xDevInfo),sizeof(TG_DEV_INFO) ) )
			{
				pCamera = m_arCamera[i];
				break;
			}
		}
	}
	return pCamera;
}


int TransMan::DoRealStream( TG_SVR_RS_INFO *pRsInfo )
{
	int nRet = TG_ERR_FAILURE;

	TransRealStream* pCamera = NULL;

	SINT16 nCameraPos = GetCameraPos( &pRsInfo->xDevInfo );
	if( nCameraPos >= 0 )
	{
		TG_AUTO_LOCK xLock(&m_xCameraSect[nCameraPos]); 

		nRet = m_arCamera[nCameraPos]->AddItem( pRsInfo );

		UINT16 nCount = 0;
		m_arCamera[nCameraPos]->GetItemCount( nCount );
		if( 0 == nCount )
		{
			m_arCamera[nCameraPos]->StopRealStream();
			MySafeDelete( m_arCamera[nCameraPos] );
		}
	}
	else if( 1 == pRsInfo->nEnable )
	{
		UINT16 i=0;
		pCamera = NULL;
		for(i=0;i<m_nUseCount;i++)
		{
			pCamera = m_arCamera[i];
			if( NULL == pCamera)
			{
				nCameraPos = i;
				break;
			}
		}
		if( i >= TG_TS_MAXCAMERA )
		{
			return TG_ERR_MAX_NUM;
		}
		if( i >= m_nUseCount )
		{
			m_nUseCount += 1;
			nCameraPos = i;
		}

		TG_AUTO_LOCK xLock2(&m_xCameraSect[nCameraPos]);

		pCamera = new TransRealStream(this);
		if( NULL == pCamera || 
			TG_ERR_SUCCESS != pCamera->Initialize() )
		{
			nRet = TG_ERR_INITERROR;
			return nRet;
		}
		m_arCamera[nCameraPos] = pCamera;

		pCamera->StartRealStream(pRsInfo);

		nRet = pCamera->AddItem( pRsInfo );
	}

	return TG_ERR_SUCCESS;
}






int TransMan::UpdateServerLive()
{
	if( NULL == m_hLogin )
	{
		return -1;
	}

	int nSecond = m_nActiveTime/1000;
	if( nSecond < 1 )
	{
		nSecond = 1;
	}

	NET_SERVER_LIVE xSvrLive;
	m_xGetSysInfo.getmac_one(0,(char*)xSvrLive.cNodeID);
	xSvrLive.nNodeType   = 1;
	xSvrLive.nCmdPort    = m_nCmdPort;
	xSvrLive.nStreamPort = m_nDataPort;
	xSvrLive.nRecPort    = 0;
	xSvrLive.nCpuUsage   = m_xGetSysInfo.GetCpuPercent();;
	xSvrLive.nBindWidth  = m_xGetSysInfo.GetNetBWPercent() / nSecond;
	xSvrLive.nReserve    = 0;


	int nRet   = 0;
	int nTimes = 2;
	do 
	{
		nRet = TG_Net_SetConfig( m_hLogin, 0x2006,&xSvrLive,sizeof(NET_SERVER_LIVE)-2 );

	} while ( TG_ERR_SUCCESS != nRet && nTimes-- );

	return 0;
}



#include "TG_SvrTransSDK.h"


void TransMan::TransStop_new()
{
	
	if( NULL != m_hSvrTrans )
	{
		TG_SvrTrans_Stop( m_hSvrTrans );

		TG_SvrTrans_Cleanup( m_hSvrTrans );
		m_hSvrTrans = NULL;
	}

	
}


int TransMan::TransStart_new()
{
	int nRet = TG_ERR_FAILURE;

	do 
	{
		if( NULL == m_hSvrTrans )
		{
			TG_SvrTrans_Init( m_hSvrTrans );
		}
		if( NULL == m_hSvrTrans )
		{
			break;
		}

		TG_SvrTrans_RegRealStreamCallBack( m_hSvrTrans,SvrNetRealStreamCB,this);
		TG_SvrTrans_SetConnectTime(m_hSvrTrans,5000,20);
		TG_SvrTrans_SetListen(m_hSvrTrans,NULL, m_nCmdPort, m_nDataPort);

	


		nRet = TG_SvrTrans_Start( m_hSvrTrans );
		if( TG_ERR_SUCCESS != nRet )
		{
			break;
		}


		if( 1 == m_nCSEnable )
		{
			TG_LOGIN_INFO xLoinInfo;
			sprintf(xLoinInfo.xConnectInfo.szServerIP,m_szCServerIP);
			xLoinInfo.xConnectInfo.nServerPort = m_nCSPort;

			nRet = TG_Net_Login( &xLoinInfo, m_hLogin );
			if( TG_ERR_SUCCESS != nRet )
			{
				break;
			}

			UpdateServerLive();
		}


		return TG_ERR_SUCCESS;

	} while (FALSE);

	return nRet;
}
